package com.tobe.spring_demo.mybatis;

import java.io.IOException;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;

/**
 * @author yangqifan 2015年9月14日
 * 
 */
public class MyMapperConfigurer implements BeanDefinitionRegistryPostProcessor , ApplicationContextAware{
	
	private String basePackage;
	private ApplicationContext applicationContext;
	
	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		// TODO Auto-generated method stub
		
	}

	//注入beanDefinition到spring容器中
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		
		MyClassPathScanner scanner = new MyClassPathScanner(registry);
		scanner.setResourceLoader(applicationContext);
		
		//允许加载所有接口
		scanner.addIncludeFilter(new TypeFilter() {
		        public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
		          return true;
		        }
		});
		
		System.out.println("扫描包:" + basePackage);
		int scan = scanner.scan(basePackage);
		
		System.out.println("加载bean num = " + scan);
		
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
		
	}

}

